<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;


class WealExchange extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%weal_exchange}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public static function addData($data){
        $model = new static();
        if(!empty($data['member_id']))
            $model->member_id = $data['member_id'];
        if(!empty($data['weal_id']))
            $model->weal_id = $data['weal_id'];
        if(!empty($data['order_num']))
            $model->order_num = $data['order_num'];
        if(!empty($data['weal_title']))
            $model->weal_title = $data['weal_title'];
        return $model->save();
    }

    /**
     * @inheritdoc
     */
    public static function selectFieldData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select($field)->from('weal_exchange');
        $query->leftJoin('weal w','`weal_exchange`.`weal_id` = `w`.`id`');
        $query->where(['`weal_exchange`.`deleted_at`' => null]);
        $query->orderBy([
            '`weal_exchange`.`created_at`' => SORT_DESC,
        ]);
        if(!empty($data['member_id'])){
            $query->andWhere(['member_id' => $data['member_id']]);
        }
        if(!empty($data['type'])){
            $query->andWhere(['type' => $data['type']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        return $result;
    }



}
